<?php
require_once('./includes/configuration.php');
require_once('./includes/global.php');

class News {

    private $template;
    private $templateCom;

    public function __construct($template) {
        $this->template = './include/design/' . $template . '/templates/template_news.php';
        $this->templateCom = './include/design/' . $template . '/templates/template_comment.php';
    }

    /**
     * displayAll - Affiche les 5 dernières
     * news.
     */
    public function displayAll() {

        $Usefull = new Usefull();
        $PDO = getDbConnection();
        $limite = 5;

        $req = $PDO->prepare("SELECT * FROM  " . DB_NAME . "." . TB_NEWS . " ORDER BY ID desc LIMIT 0, " . $limite . "");
        $req->execute();
        $countNews = $req->rowCount();
        $news = $req->fetchAll();

        if ($news == null) {
              echo "<p>Aucune news n'a encore &eacute;t&eacute; r&eacute;dig&eacute;e.</p>";
        } else {
            foreach ($news as $data) {
                foreach ($data as $key => $value) {
                    $value = stripcslashes($value);
                    switch ($key) {
                        case 'id':
                            $id = $value;
                            break;
                        case 'title':
                            $title = ucfirst($value);
                            break;
                        case 'author':
                            $author = ucfirst($value);
                            break;
                        case 'content':
                            $content = ucfirst($value);
                            break;
                        case 'date':
                            $date = $Usefull->convertDate($value);
                            break;
                        case 'online':
                            $online = $value;
                            break;
                        default:

                            break;
                    }
                }
                
		
		$reqCom = $PDO->prepare("SELECT * FROM  " . DB_NAME . "." . TB_COMMENT . " WHERE id_news = '$id'");
$reqCom->execute();
$DataCom = $reqCom->rowCount();
?>
<div class="news_titre rounded">
    <div class="news_title"><?php echo $title; ?></div>
    <div class="news_info">Ajout&eacute; le: <img src="./style/images/icone/date.png" alt="date" /> <?php echo $date; ?>, par <b><a href="<?php echo $_SERVER['PHP_SELF'] . "?page=user&user=" . $author ?>" class="mainlinks" title="Voir le profil de <?php echo $author; ?>"><img src="<?php echo DESIGNROOT . $siteTheme . "/images/icone/author.png"; ?>" alt="auteur" /> <?php echo $author; ?></a></b></div> <br />
</div>
<div class="news_txt">
    <?php echo $content; ?> <br />
    <br />
    <i><a href="<?php echo $_SERVER['PHP_SELF']; ?>?page=comment&news=<?php echo $id; ?>" class="px10" title="Lire les commentaires"><img src="<?php echo DESIGNROOT . $siteTheme . "/images/comment/comments.png"; ?>" /> Lire les commentaires (<?php echo $DataCom; ?>)</a></i> <br />
</div>
<hr />
<br />

<?php
		
            }
            echo "<br />";
            echo "<b class=\"px10\"><a href=\"./index.php?page=archives\">Voir les archives (" . $countNews . ")</a></b>";
        }
    }

    /**
     * displayAllArchive - Affiche toutes les news
     * limité a 5 par page.
     */
    public function displayAllArchive($asc = true, $month = null) {
        if ($month == null) {
            $month = date('n');
        }
        $Usefull = new Usefull();
        $PDO = getDbConnection();
        $limite = 5;
        if ($asc == true) {
            $req = $PDO->prepare("SELECT * FROM  " . DB_NAME . "." . TB_NEWS . " WHERE date LIKE '_____" . $month . "___' ORDER BY id DESC");
        } else if ($asc == false) {
            $req = $PDO->prepare("SELECT * FROM  " . DB_NAME . "." . TB_NEWS . " WHERE date LIKE '_____" . $month . "___' ORDER BY id ASC");
        }
        $req->execute();
        $news = $req->fetchAll();

        if ($news == null) {
              echo "<p>Aucune news n'a encore &eacute;t&eacute; r&eacute;dig&eacute;e.</p>";
        } else {
            foreach ($news as $data) {
                foreach ($data as $key => $value) {
                    $value = stripcslashes($value);
                    switch ($key) {
                        case 'id':
                            $id = $value;
                            break;
                        case 'title':
                            $title = ucfirst($value);
                            break;
                        case 'author':
                            $author = ucfirst($value);
                            break;
                        case 'content':
                            $content = ucfirst($value);
                            break;
                        case 'date':
                            $date = $Usefull->convertDate($value);
                            break;
                        case 'online':
                            $online = $value;
                            break;
                        default:

                            break;
                    }
                }
                if ($online == 1) {
                    require($this->template);
                }
            }
        }
    }

    /**
     * displaySelected - Affiche la news en
     * fonction de l'id passé en argument ($id).
     */
    public function displaySelected($id) {

        $Usefull = new Usefull();
        $PDO = getDbConnection();
        $limite = 5;

        $req = $PDO->prepare("SELECT * FROM  " . DB_NAME . "." . TB_NEWS . " WHERE id = " . $id . "");
        $req->execute();
        $news = $req->fetchAll();

        if ($news == null) {
            Usefull::displayPopUp("error", "La news s&eacute;l&eacute;ctionn&eacute;e, n'existe pas.");
        } else {
            foreach ($news as $data) {
                foreach ($data as $key => $value) {
                    $value = stripcslashes($value);
                    switch ($key) {
                        case 'id':
                            $id = $value;
                            break;
                        case 'title':
                            $title = ucfirst($value);
                            break;
                        case 'author':
                            $author = ucfirst($value);
                            break;
                        case 'content':
                            $content = ucfirst($value);
                            break;
                        case 'date':
                            $date = $Usefull->convertDate($value);
                            break;
                        default:

                            break;
                    }
                }
                require($this->templateCom);
            }
        }
    }

    public function displayComment($id) {

        $Usefull = new Usefull();
        $PDO = getDbConnection();
        $image = "";

        $req = $PDO->prepare("SELECT * FROM  " . DB_NAME . "." . TB_COMMENT . " WHERE id_news = " . $id . "");
        $req->execute();
        $comment = $req->fetchAll();

        if ($comment == null) {
            Usefull::displayPopUp("error", "Aucun commentaire n'a encore &eacute;t&eacute; post&eacute;.");
        } else {
            foreach ($comment as $data) {
                foreach ($data as $key => $value) {
                    $value = stripcslashes($value);
                    switch ($key) {
                        case 'id':
                            $id = $value;
                            break;
                        case 'author':
                            $author = ucfirst($value);
                            break;
                        case 'comment':
                            $content = $Usefull->convertChar(ucfirst($value));
                            break;
                        case 'date':
                            $date = $Usefull->convertDate($value);
                            break;
                        case 'hour':
                            $hour = $Usefull->convertHour($value);
                            break;
                        default:

                            break;
                    }
                }
                $reqImg = $PDO->prepare("SELECT image, facebook, twitter, google FROM " . DB_NAME . "." . TB_UTILISATEURS . " WHERE login = ?");
                $reqImg->bindValue(1, $author, PDO::PARAM_STR);
                $reqImg->execute();
                $getImg = $reqImg->fetchAll();
                foreach ($getImg as $dataImg) {
                    foreach ($dataImg as $keyImg => $valueImg) {
                        switch ($keyImg) {
                            case 'image':
                                $image = $valueImg;
                                break;
                            case 'facebook':
                                $facebook = $valueImg;
                                break;
                            case 'twitter':
                                $twitter = $valueImg;
                                break;
                            case 'google':
                                $google = $valueImg;
                                break;
                        }
                    }
                }
                if ($image == "") {
                    $image = "unknow.png";
                }
                ?>
                <div class="commentBox">
                    <div class="commentProfil">
                        <a href="<?php echo "./index.php?page=user&user=" . $author; ?>" title="<?php echo "Voir le profil de " . $author; ?>">
                            <img src="<?php echo DESIGNROOT . "avatar/" . $image; ?>" alt="<?php echo $author; ?>" title="<?php echo "Voir le profil de " . $author; ?>" /> <br />
                            <p>
                                <?php echo $author; ?>
                            </p>
                        </a>
                    </div>
                    <div class="commentInfo">
                        <?php
                        if ($facebook != "" || $twitter != "" || $google != "") {
                            if ($facebook != "" && $twitter == "" && $google == "") {
                                echo "<p class=\"left\">Post&eacute; le " . $date . " &agrave; " . $hour . ".</p><p class=\"right\"\><a href=\"" . $facebook . "\" title=\"Son profil Facebook\" target=\"_BLANK\"><img src=\"./include/design/default/images/icone/facebook.png\" alt=\"Facebook\" /></a></p>";
                            } else if ($facebook == "" && $twitter != "" && $google == "") {
                                echo "<p class=\"left\">Post&eacute; le " . $date . " &agrave; " . $hour . ".</p><p class=\"right\"\><a href=\"" . $twitter . "\" title=\"Son profil Twitter\" target=\"_BLANK\"><img src=\"./include/design/default/images/icone/twitter.png\" alt=\"Twitter\" /></a></p>";
                            } else if ($facebook != "" && $twitter != "" && $google == "") {
                                echo "<p class=\"left\">Post&eacute; le " . $date . " &agrave; " . $hour . ".</p><p class=\"right\"\><a href=\"" . $facebook . "\" title=\"Son profil Facebook\" target=\"_BLANK\"><img src=\"./include/design/default/images/icone/facebook.png\" alt=\"Facebook\" /></a> <a href=\"" . $twitter . "\" title=\"Son profil Twitter\" target=\"_BLANK\"><img src=\"./include/design/default/images/icone/twitter.png\" alt=\"Twitter\" /></a></p>";
                            } else if ($facebook != "" && $twitter == "" && $google != "") {
                                echo "<p class=\"left\">Post&eacute; le " . $date . " &agrave; " . $hour . ".</p><p class=\"right\"\><a href=\"" . $facebook . "\" title=\"Son profil Facebook\" target=\"_BLANK\"><img src=\"./include/design/default/images/icone/facebook.png\" alt=\"Facebook\" /></a> <a href=\"" . $google . "\" title=\"Son profil Google+\" target=\"_BLANK\"><img src=\"./include/design/default/images/icone/google.png\" alt=\"Google\" /></a></p>";
                            } else if ($facebook == "" && $twitter != "" && $google != "") {
                                echo "<p class=\"left\">Post&eacute; le " . $date . " &agrave; " . $hour . ".</p><p class=\"right\"\><a href=\"" . $twitter . "\" title=\"Son profil Twitter\" target=\"_BLANK\"><img src=\"./include/design/default/images/icone/twitter.png\" alt=\"Twitter\" /></a> <a href=\"" . $google . "\" title=\"Son profil Google+\" target=\"_BLANK\"><img src=\"./include/design/default/images/icone/google.png\" alt=\"Google\" /></a></p>";
                            } else if ($facebook != "" && $twitter != "" && $google != "") {
                                echo "<p class=\"left\">Post&eacute; le " . $date . " &agrave; " . $hour . ".</p><p class=\"right\"\><a href=\"" . $facebook . "\" title=\"Son profil Facebook\" target=\"_BLANK\"><img src=\"./include/design/default/images/icone/facebook.png\" alt=\"Facebook\" /></a> <a href=\"" . $twitter . "\" title=\"Son profil Twitter\" target=\"_BLANK\"><img src=\"./include/design/default/images/icone/twitter.png\" alt=\"Twitter\" /></a> <a href=\"" . $google . "\" title=\"Son profil Google+\" target=\"_BLANK\"><img src=\"./include/design/default/images/icone/google.png\" alt=\"Google\" /></a></p>";
                            }
                        } else {
                            echo "Post&eacute; le " . $date . " &agrave; " . $hour . ".";
                        }
                        ?>
                    </div>
                    <div class="commentContent">
                        <?php echo $content; ?>
                    </div>
                </div>
                <br />
                <?php
            }
        }
    }

    function displayTabNews() {
        global $siteTheme;
        $PDO = getDbConnection();
        $Usefull = new Usefull();
        $req = $PDO->prepare("SELECT * FROM " . DB_NAME . "." . TB_NEWS . " ORDER BY id desc");
        $req->execute();
        $news = $req->fetchAll();

        echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\" id=\"pattern-style-b\" summary=\"Toutes les news\" class='zebra-striped'>";
        echo "<colgroup>" . "\n\t\t";
        echo "<col width=\"160px\">";
        echo "<col width=\"50px\">";
        echo "<col width=\"160px\">";
        echo "<col width=\"40px\">";
        echo "<col width=\"160px\">";
        echo "<col width=\"5px\">";
        echo "<col width=\"5px\">";
        echo "</colgroup>";
        echo "<thead><th scope=\"col\">Titre</th><th scope=\"col\">Auteur</th><th scope=\"col\">Texte</th><th scope=\"col\">Date</th><th>&Eacute;tat</th><th colspan=\"2\" class=\"Option\">Options</th></thead><tbody>";
        if ($news == null) {
            echo "<tr align=\"center\"><td colspan=\"7\">Aucune news n'a encore &eacute;t&eacute; ajout&eacute;e</td></tr>";
        } else {
            foreach ($news as $data) {
                foreach ($data as $key => $value) {
                    $value = stripcslashes($value);
                    switch ($key) {
                        case 'id':
                            $id = $value;
                            break;
                        case 'title':
                            $title = ucfirst($value);
                            break;
                        case 'author':
                            $author = ucfirst($value);
                            break;
                        case 'content':
                            $content = substr(ucfirst($value), 0, 20);
                            break;
                        case 'date':
                            $date = $Usefull->convertDate($value);
                            break;
                        case 'online':
                            if ($value == 1) {
                                $online = "<span class=\"label success\">En ligne</span>";
                            } else {
                                $online = "<span class=\"label important\">Hors-ligne</span>";
                            }
                            break;
                    }
                }
                echo "<tr><td class=\"news\">";
                echo $title;
                echo "</td><td class=\"news\">";
                echo $author;
                echo "</td><td class=\"news\">";
                echo $content;
                echo "</td><td class=\"news\">";
                echo $date;
                echo "</td><td class=\"news\">";
                echo $online;
                echo "</td><td class=\"btnOption news\">";
                echo "<form method=\"post\" action=\"./admin.php?page=news\" ><input type=\"hidden\" name=\"del_id\" value=\"" . $id . "\"><input type='image' src=\"./include/design/" . $siteTheme . "/images/icone/delete_little.png\" name='del_news' value='Supprimer' alt='Supprimer' title='Supprimer' class='btnDelete'></form></td><td class=\"btnOption news\">";
                echo "<form method=\"post\" action=\"./admin.php?page=news\" ><input type=\"hidden\" name=\"edit_id\" value=\"" . $id . "\"><input type='image' src=\"./include/design/" . $siteTheme . "/images/icone/edit_little.png\" name='edit_news' value='Editer' alt='Editer' title='Editer' class='btnEdit'></form>";
                echo "</td></tr>";
            }
        }
        echo "</tbody></table>";
        echo "<form method=\"post\" //action=\"./admin.php?page=news\"><input type=\"submit\" name=\"add\" value=\"Ajouter une news\" class=\"btn primary\"> <a href=\"./admin.php?page=index&tab=gestion\" class=\"btn\" title=\"Retourner au PAD\">Retour</a></form>";
    }

}
?>
